var _ = require('lodash')
import RegisterUtil from './registerUtil'

export default {
  // 更新节点属性
  refreshNodeProp(component, node) {
    component.$store.commit('refreshNodeProp', node)
  },

  // 检查节点的selector属性
  checkSelectorAttr(nodes) {
    nodes.forEach((node, index) => {
      if (!node.data) {
        node.data = {}
      }
      if (!node.data.selector) {
        node.data.selector = node.shape + index
      }
    })
  },

  // 特殊处理polygon和polyline节点
  dealPolygonAndPolyline(nodes, graph) {
    // 检查节点中是否存在polygon和polyline节点，如果存在需要为这些节点添加一个隐藏边框
    var refNodes = []
    for (const node of nodes) {
      if (node.shape === 'polygon' || node.shape === 'polyline') {
        var size = node.size()
        var position = node.position()
        var selector = node.data.selector
        var refNode = graph.addNode({
          shape: 'rect',
          x: position.x,
          y: position.y,
          width: size.width,
          height: size.height,
          zIndex: -1,
          attrs: {
            body: {
              fill: 'none',
              stroke: 'none'
            }
          }
        })
        refNode.data = {}
        refNode.data.selector = selector + '-ref'
        refNodes.push(refNode)
      }
    }
    refNodes.forEach((refNode) => {
      nodes.push(refNode)
    })
  },

  // 创建自定义节点
  createCustomNode(component, graph, formConfig) {
    var nodes = graph.getSelectedCells()
    if (nodes.length === 0) {
      return
    }

    const self = this
    // 检查节点是否配置了selector属性
    self.checkSelectorAttr(nodes)

    // 检查节点中是否存在polygon和polyline节点，如果存在需要为这些节点添加一个隐藏边框
    self.dealPolygonAndPolyline(nodes, graph)

    // 为所有节点创建一个父节点，用于自定义节点时获取相对位置
    const nodeSize = RegisterUtil.getNodeSize(nodes)
    var parentNode = graph.addNode({
      shape: 'rect',
      x: nodeSize.minX,
      y: nodeSize.minY,
      width: nodeSize.width,
      height: nodeSize.height,
      zIndex: -1
    })
    nodes.forEach((node) => {
      parentNode.addChild(node)
    })

    try {
      RegisterUtil.registerCustomNode(component, formConfig, parentNode)
      component.$message({ title: 'success', message: '注册节点成功' })
    } catch (e) {
      console.log('注册节点时发生错误', e)
      component.$message({ title: 'warning', message: '注册节点失败' })
    }
  },

  // 获取edge所连接的图元节点
  getNodeByEdge(graph, edge, isSource) {
    const node = isSource ? edge.getSource() : edge.getTarget()
    if (node && node.cell) {
      return graph.getCell(node.cell)
    }
  },

  // 添加junctionInfo信息
  addJunctionInfo(graph, edge) {
    const edgeFlagId = edge.getData().flagId
    let edgeJunctionInfo = ''
    const source = this.getNodeByEdge(graph, edge, true)
    if (source) {
      this.addJunctionInfo2Node(source, edgeFlagId)
    }
    edgeJunctionInfo += source ? source.getData().flagId : 'null'
    edgeJunctionInfo += ','

    const target = this.getNodeByEdge(graph, edge, false)
    if (target) {
      this.addJunctionInfo2Node(target, edgeFlagId)
    }
    edgeJunctionInfo += target ? target.getData().flagId : 'null'

    edge.setData({
      junctionInfo: edgeJunctionInfo
    })
  },

  addJunctionInfo2Node(node, edgeFlagId) {
    const data = node.getData()
    let junctionInfo = _.get(data, 'junctionInfo', '')
    if (junctionInfo.indexOf(edgeFlagId) !== -1) {
      return
    }
    junctionInfo += _.isEmpty(junctionInfo) ? '' : ':-:'
    junctionInfo += '[](' + edgeFlagId + ')'
    node.setData({
      junctionInfo: junctionInfo
    })
  },

  // 移除junctionInfo信息
  removeJunctionInfo(graph, edge) {
    const edgeFlagId = edge.getData().flagId
    const source = this.getNodeByEdge(graph, edge, true)
    if (source) {
      this.removeJunctionInfoFromNode(source, edgeFlagId)
    }
    const target = this.getNodeByEdge(graph, edge, false)
    if (target) {
      this.removeJunctionInfoFromNode(target, edgeFlagId)
    }
    edge.setData({
      junctionInfo: ''
    })
  },

  removeJunctionInfoFromNode(node, edgeFlagId) {
    const data = node.getData()
    let junctionInfo = _.get(data, 'junctionInfo', '')
    if (junctionInfo.indexOf(edgeFlagId) === -1) {
      return
    }
    junctionInfo = _.replace(junctionInfo, ':-:[](' + edgeFlagId + ')', '')
    junctionInfo = _.replace(junctionInfo, '[](' + edgeFlagId + ')', '')
    node.setData({
      junctionInfo: junctionInfo
    })
  }
}
